home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / aijournl / 1986_09 / ops83.ltg < prev    next >
Text File  |  1986-10-30  |  8KB  |  239 lines

  1.  
  2.  
  3.                             Listing 1
  4.  
  5.    A demonstration program written in OPS83 to solve extremely 
  6.                simple DC circuits using Ohm's Law.
  7.  
  8.  
  9. module ohms(start_ohms)
  10. {
  11. use shell;
  12. use utils;
  13.  
  14. --First we have to model the circuit.
  15.  
  16. --Devices look like this:
  17.  
  18. type device = element
  19.   (type : symbol;    -- resistor, voltsource
  20.    tag : symbol;     -- tag identifies each unique device
  21.    i : real;         --current through device
  22.    istat : symbol;   --status of i, solved or unknown
  23.    val : real;       -- value of device in ohms or volts
  24.    valstat : symbol; --is value known?
  25.    );
  26.  
  27. --All devices are connected to junctions.  Two devices connected
  28. --to the same junction are connected to each other.  
  29.  type connect = element
  30.    (dev : symbol;
  31.     trm   : integer;
  32.     junct : symbol;);  
  33.  
  34. --All junctions have a voltage.  Implicitly, this means that
  35. --all terminals at a junction are at the same potential.
  36.  
  37. type junct = element
  38.    (tag : symbol;
  39.     volts : real;
  40.     vstat : symbol);
  41.  
  42.  
  43. -- An equivalent resistor is one which is substituted for 
  44. -- a resistor using one of the combining forms.  The values
  45. -- and connections of the replaced resistors are stored in
  46. -- the wme for future use.
  47.  
  48. type equiv_r = element
  49. (type : symbol;
  50.  tag : symbol;
  51.  r1 : symbol; -- connections for r1
  52.  r1t1 : integer; -- terminals
  53.  r1t2 : integer;
  54.  r1j1 : symbol;  -- junctions
  55.  r1j2 : symbol;
  56.  r2 : symbol;  -- connections for r2
  57.  r2t1 : integer;
  58.  r2t2 : integer;
  59.  r2j1 : symbol;
  60.  r2j2 : symbol;
  61.  );
  62.  
  63. --The start procedure.  In this procedure, the circuit is modeled, 
  64. --and whatever values are known are set.
  65. procedure start_ohms()
  66. {
  67.  make (device type=voltsource; tag=v1;val=12.0;i=12.0;istat=unknown);
  68.  make (device type=resistor;tag=r1;val=10.0;valstat=known;i=12.0;istat=unknown);
  69.  make (device type=resistor;tag=r2;val=4.0;i=12.0;valstat=known;istat=unknown);
  70.  make (device type=resistor;tag=r3;val=2.0;valstat=known;i=12.0;istat=unknown);
  71.  make (device type=resistor;tag=r4;val=4.0;valstat=known;istat=unknown);
  72.  make (device type=resistor;tag=r5;val=8.0;valstat=known;i=12.0;istat=unknown);
  73.  make (device type=resistor;tag=r6;val=8.0;valstat=known;istat=unknown);
  74.  make (connect  dev=v1;trm=1;junct=j1);
  75.  make (connect  dev=v1;trm=2;junct=j4);
  76.  make (connect  dev=r1;trm=1;junct=j1);
  77.  make (connect  dev=r1;trm=2;junct=j2);
  78.  make (connect  dev=r2;trm=1;junct=j2);
  79.  make (connect  dev=r3;trm=1;junct=j2);
  80.  make (connect  dev=r2;trm=2;junct=j4);
  81.  make (connect  dev=r3;trm=2;junct=j3);
  82.  make (connect  dev=r4;trm=1;junct=j3);
  83.  make (connect  dev=r5;trm=1;junct=j3);
  84.  make (connect  dev=r6;trm=1;junct=j3);
  85.  make (connect  dev=r4;trm=2;junct=j4);
  86.  make (connect  dev=r5;trm=2;junct=j4);
  87.  make (connect  dev=r6;trm=2;junct=j4);
  88.  make (junct volts=12.0;vstat=known;tag=j1);
  89.  make (junct vstat=unknown;tag=j2);
  90.  make (junct vstat=unknown;tag=j3);
  91.  make (junct volts=0.0;vstat=known;tag=j4);
  92.  call run(100);  -- run the production system
  93. };
  94.  
  95.  
  96. --Some simple rules derived from Ohms Law
  97.  
  98. --If you know the voltage across a device, and the resistance of the
  99. --device, than you know the current through the device
  100. rule compute_current
  101.  {  &1 (device type=resistor;istat=unknown;valstat=known);  
  102.     &2 (connect  dev=&1.tag;trm=1); -- get the junctions involved
  103.     &3 (junct tag=&2.junct;vstat=known);
  104.     &4 (connect  dev=&1.tag;trm=2);
  105.     &5 (junct tag=&4.junct;vstat=known);
  106. -->
  107.      -- I = V/R
  108.      -- Establish convention, current flows from t1 to t2 in 
  109.      -- each device.  A negative sign indicates the inverse.
  110.        modify &1 (i= (&3.volts - &5.volts)/&1.val;istat=known)
  111.      }; 
  112.  
  113. --If you know the resistance of a device and the current 
  114. --through that device, then you can compute the voltage drop
  115. --across the device.
  116.  
  117. rule compute_voltage
  118.  {  &1 (device type=resistor;istat=known;valstat=known);  
  119.     &2 (connect  dev=&1.tag); -- get the junctions involved
  120.     &3 (junct tag=&2.junct;vstat=unknown);
  121.     &4 (connect  dev=&1.tag;junct<>&2.junct);
  122.     &5 (junct tag=&4.junct;vstat=known); -- one is unknown
  123. -->
  124.      -- (V1 - V2) = IR
  125.      modify &3 (volts = &1.val * &1.i + &5.volts; vstat=known);
  126.      };
  127.  
  128. --If you know the voltage across a device, and the current 
  129. --through the device, then you can compute the resistance. 
  130. rule compute_resistance
  131.  {  &1 (device type=resistor;istat=known;valstat=unknown);  
  132.     &2 (connect  dev=&1.tag;trm=1); -- get the junctions involved
  133.     &3 (junct tag=&2.junct;vstat=known);
  134.     &4 (connect  dev=&1.tag;trm=2);
  135.     &5 (junct tag=&4.junct;vstat=known);
  136. -->
  137.      -- R = V/I
  138.      modify &1 (val = (&3.volts - &5.volts)/&1.i;valstat=known);
  139.      };
  140.  
  141. --Resistor manipulations
  142.  
  143. -- Two resistors in series can be replaced by one resistor with 
  144. -- value R1 + R2
  145.  
  146. rule replace_series
  147. {   &1  (device type=resistor;istat=unknown;valstat=known); --find device
  148.     &2  (connect dev=&1.tag;trm=2);
  149.     &3  (device type=resistor;istat=unknown;valstat=known;tag<>&1.tag);
  150.     &4  (connect dev=&3.tag;junct=&2.junct); --find common junction
  151. --no other device connected to junction (resistors are in series)
  152.        ~(connect junct=&2.junct;(@.dev <> &3.tag /\ @.dev <> &1.tag));
  153.        -- find other connections to these devices
  154.     &5  (connect dev=&1.tag;junct<>&2.junct);
  155.     &6  (connect dev=&3.tag;junct<>&4.junct);
  156.        -->
  157.        local &equiv : symbol;
  158.        &equiv = gensym(equiv);
  159.        make(equiv_r type=series;tag=≡r1=&1.tag;
  160.     r1t1=&5.trm;r1t2=&4.trm;
  161.     r1j1=&5.junct;r1j2=&4.junct;
  162.     r2t1=&6.trm;r2t2=&4.trm;
  163.     r2j1=&6.junct;r2j2=&4.junct;
  164.     r2=&3.tag);
  165.        make(device type=resistor;tag=≡istat=unknown;
  166.    val = &1.val + &3.val;valstat=known);
  167.        --substitute new device into circuit
  168.        modify &5 (dev=≡trm=1);
  169.        modify &6 (dev=≡trm=2);
  170.        remove &2;  -- disconnect replaced resistors
  171.        remove &4;
  172.        };
  173.  
  174.  
  175. -- Two resistors in parallel can be replaced by one resistor with
  176. -- value (R1 * R2)/(R1 + R2)
  177.  
  178. rule replace_parallel
  179. {   &1  (device type=resistor;istat=unknown;valstat=known); --find device
  180.     --find both connections to that device.
  181.     &2  (connect dev=&1.tag;trm=1);
  182.     &3  (connect dev=&1.tag;trm=2);
  183.     --now, find another device connected to both junctions
  184.     &4  (device type=resistor;istat=unknown;valstat=known;tag<>&1.tag);
  185.     &5  (connect dev=&4.tag;junct=&2.junct); --find common junction
  186.     &6  (connect dev=&4.tag;junct=&3.junct); --find common junction
  187.        -->
  188.        local &equiv : symbol;
  189.        &equiv = gensym(equiv);
  190.        make(equiv_r type=parallel;tag=≡r1=&1.tag;
  191.     r1t1=&2.trm;r1t2=&3.trm;
  192.     r1j1=&2.junct;r1j2=&3.junct;
  193.     r2t1=&5.trm;r2t2=&6.trm;
  194.     r2j1=&5.junct;r2j2=&6.junct;
  195.     r2=&4.tag);
  196.        make(device type=resistor;tag=≡istat=unknown;
  197.    val = (&1.val * &4.val)/(&1.val + &4.val);valstat=known);
  198.        --substitute new device into circuit
  199.        modify &5 (dev=≡trm=1);
  200.        modify &6 (dev=≡trm=2);
  201.        remove &2;
  202.        remove &3; 
  203.        };
  204.  
  205.  
  206. --After an equivalent resistor has been solved, it can be replaced
  207. --by its constituent parts
  208.  
  209. rule replace_equiv  --replace equivalent resistor
  210. {  &1 (equiv_r ); -- find an equivalent resistor
  211.    &2 (device tag=&1.tag;istat=known;valstat=known); --which is solved
  212.    &3 (connect dev=&2.tag);
  213.    &4 (junct tag=&3.junct;vstat=known);
  214.    &5 (connect dev=&2.tag;junct<>&3.junct);
  215.    &6 (device tag=&1.r1);
  216.    &7 (device tag=&1.r2);
  217.    -->
  218.    if (&1.type = series)
  219.       {
  220.        --current in a series circuit is current through equivalent resistor
  221.        modify &6 (istat=&2.istat;i=&2.i);
  222.        modify &7 (istat=&2.istat;i=&2.i)}
  223.      else {
  224.       --current in a parallel circuit is I1 = Iequiv*R2/(R1+R2)
  225.      modify &6 (istat=&2.istat;i=&2.i * &7.val/(&7.val + &6.val));
  226.      modify &7 (istat=&2.istat;i=&2.i * &6.val/(&7.val + &6.val))};
  227.    remove &2; -- remove equivalent resistor
  228.    --reconnect original resistors
  229.    make (connect dev=&1.r1;junct=&1.r1j1;trm=&1.r1t1);
  230.    make (connect dev=&1.r1;junct=&1.r1j2;trm=&1.r1t2);
  231.    make (connect dev=&1.r2;junct=&1.r2j1;trm=&1.r2t1);
  232.    make (connect dev=&1.r2;junct=&1.r2j2;trm=&1.r2t2);
  233.    remove &5; -- remove connections for equiv resistor
  234.    remove &3;
  235.    };
  236.  
  237.  
  238.    };  -- end of module
  239.